package com.crazy.appium.utils;

import java.io.ByteArrayOutputStream;

import org.apache.commons.exec.CommandLine;

import org.apache.commons.exec.DefaultExecutor;

import org.apache.commons.exec.PumpStreamHandler;

/**
 * @author 沙陌 qq2879897713
 *
 */
public class CommandLineExec {
	private static Log logger = Log.getLogger(CommandLineExec.class);
	public static String osName = System.getProperty("os.name");

	public static String executor(String command) throws Exception {
		try {
			String commandBasic = "cmd /c ";
			if (Command.isMac()) {
				commandBasic = "";
			}
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
			CommandLine commandline = CommandLine.parse(commandBasic+command);
			DefaultExecutor exec = new DefaultExecutor();
			exec.setExitValues(null);
			PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream);
			exec.setStreamHandler(streamHandler);
			exec.execute(commandline);
			String out = outputStream.toString("gbk");
			String error = errorStream.toString("gbk");
			if (!"".equals(error.trim())) {
				throw new Exception("executore " + command + "failure-->" + error);
			}
			return out;

		} catch (Exception e) {
			logger.error("executore " + command + " failure-->" + e.getMessage());
			throw new Exception("executore " + command + " failure-->" + e.getMessage());
		}
	}

	public static void main(String[] args) throws Exception{
		//System.out.println(executor("adb connect 127.0.0.1:62001"));
			System.out.println(executor("adb devices"));
	}

}
